import { defineStore } from "pinia";
import type { StoreDefinition } from "pinia";
import { ElNotification } from "element-plus";

type ErrorMessageType = { message: string, stack: string };

type SystemStoreState = {
    errors: ErrorMessageType[];
};

export const useStoreSystemError: StoreDefinition<string, SystemStoreState> =
    defineStore<string, SystemStoreState>("systemError", {
        persist: true,
        state: (): SystemStoreState => ({
            errors: []
        }),

        actions: {
            /**
             * @description 更新语言
             * */
            updateSystemError(value: ErrorMessageType) {
                this.errors.unshift(value);
                ElNotification({
                    title: value.message,
                    message: value.stack
                });
            }
        },

        getters: {
            getSystemError(state) {
                return state.errors;
            }
        }
    });
